//
//  Car_ContrastCarViewController.m
//  CarUser
//
//  Created by ayi on 2019/1/17.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "Car_ContrastCarViewController.h"

#import "TabviewViewModel.h"
#import "Car_ContrastCarTableViewCell.h"

#import <YYCategories/UITableView+YYAdd.h>
#import <YYCategories/NSArray+YYAdd.h>

#import "Car_ContrastConsequenceViewController.h"

@interface Car_ContrastCarViewController ()<TabviewViewModelDelegate>

@property (nonatomic, strong) TabviewViewModel *viewmodel;
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIView *bottomview;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *contrastBtn;

@property (nonatomic, strong) NSMutableArray *chooseAry;

@end

@implementation Car_ContrastCarViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
//        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
//        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.chooseAry = [NSMutableArray array];
    
    self.view.backgroundColor = UIColor.blackColor;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"对比" style:UIBarButtonItemStylePlain target:self action:@selector(nullAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(clearBtnAction)];
    [self.navigationController.navigationBar setCustomBgColor:UIColor.blackColor];
    
    _viewmodel = [[TabviewViewModel alloc]init];
    _viewmodel.rowHeight = UITableViewAutomaticDimension;
    _viewmodel.sectionFooterHeight = 0;
    _viewmodel.sectionHeaderHeight = 10;
    _viewmodel.sectionFooterColor = UIColor.clearColor;
    _viewmodel.sectionHeaderColor = UIColor.clearColor;
    _viewmodel.childtab = self.tableview;
    _viewmodel.childvc = self;
    _viewmodel.delegate = self;
    
    //列表
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    
    //底部按钮
    _bottomview = [[UIView alloc]init];
    _bottomview.backgroundColor = UIColor.clearColor;
    _backBtn = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = FONT_SIZE(14);
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:Common_BG_yellow forState:UIControlStateNormal];
        btn.backgroundColor = Common_NavigroundColor;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 22;
        btn.frame = CGRectMake(BOUNDWIDTH/2 - 10 - 80, 10, 80, 44);
        [btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    _contrastBtn = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = FONT_SIZE(14);
        [btn setTitle:@"对比" forState:UIControlStateNormal];
        [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
        btn.backgroundColor = Common_BG_yellow;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 22;
        btn.frame = CGRectMake(BOUNDWIDTH/2 + 10, 10, 80, 44);
        [btn addTarget:self action:@selector(contrastBtnAction) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [_bottomview addSubview:_backBtn];
    [_bottomview addSubview:_contrastBtn];
    [self.view addSubview:_bottomview];
    CGFloat bottomHeight = TABBAR_HEIGHT + 30;
    [_bottomview mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.left.and.right.and.bottom;
        make.height.mas_equalTo(bottomHeight);
    }];
    
    UIImageView *effectview = [[UIImageView alloc]init];
    effectview.tag = 1002;
    effectview.image = [CommonMethod coreGraphicsColorGradualchange:CGSizeMake(BOUNDWIDTH,bottomHeight) fromColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0] toColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    effectview.frame  = CGRectMake(0, 0, BOUNDWIDTH, bottomHeight);
    [_bottomview addSubview:effectview];
    [_bottomview insertSubview:effectview atIndex:0];
    
//    effectview.transform = CGAffineTransformMakeScale(1.0,-1.0);
    _tableview.contentInset = UIEdgeInsetsMake(0, 0, bottomHeight, 0);
    
    
    self.viewmodel.listAry = [CarDetailMainModel contrastList];
    [self.tableview reloadData];
}





- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setCustomBgColor:UIColor.blackColor];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}



- (void)nullAction{
    
}

- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clearBtnAction{
    [CarDetailMainModel clearContrastList];
    self.viewmodel.listAry = [CarDetailMainModel contrastList];
    [self.tableview reloadData];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Car_CarDetailViewControllerFreshContrast" object:nil];
}

- (void)contrastBtnAction{
    if (self.chooseAry.count == 2 ) {
        Car_ContrastConsequenceViewController *push = [[Car_ContrastConsequenceViewController alloc]init];
        push.modelAry = self.chooseAry;
        [self.navigationController pushViewController:push animated:YES];
    }
}



#pragma mark -------TabviewViewModelDelegate
- (UITableViewCell *)vm_tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Car_ContrastCarTableViewCell *cell = [Car_ContrastCarTableViewCell initCellWithReuseIdentifier:@"Car_ContrastCarTableViewCell" andTab:tableView];
    cell.model = self.viewmodel.listAry[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)vm_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath andModel:(CarDetailMainModel *)model{
    if (model.hasChoose) {
        model.hasChoose = NO;
        Car_ContrastCarTableViewCell *cell = [self.tableview cellForRowAtIndexPath:indexPath];
        cell.chooseBtn.selected = model.hasChoose;
        [self.chooseAry removeObject:model];
    }else{
        if (self.chooseAry.count >= 2) {
            [self.chooseAry enumerateObjectsUsingBlock:^(CarDetailMainModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                obj.hasChoose = NO;
            }];
            CarDetailMainModel *onemodel = self.chooseAry.lastObject;
            onemodel.hasChoose = YES;
            
            [self.chooseAry removeAllObjects];
            [self.chooseAry addObject:onemodel];
        }
        model.hasChoose = YES;
        [self.chooseAry addObject:model];
        [self.tableview reloadData];
    }
}

/**
 初始化
 
 */
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.backgroundColor = UIColor.clearColor;
        _tableview.dataSource = self.viewmodel;
        _tableview.delegate = self.viewmodel;
        _tableview.rowHeight = UITableViewAutomaticDimension;
        _tableview.estimatedRowHeight = 100;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.tableFooterView = [UIView new];
        _tableview.showsVerticalScrollIndicator = NO;
    }return _tableview;
}


@end
